package com.cucn.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cucn.commonutils.JwtUtils;
import com.cucn.commonutils.Result;
import com.cucn.commonutils.ordervo.CourseWebVoOrder;
import com.cucn.eduservice.client.OrdersClient;
import com.cucn.eduservice.entity.EduCourse;
import com.cucn.eduservice.entity.chapter.ChapterVO;
import com.cucn.eduservice.entity.frontvo.CourseFrontVO;
import com.cucn.eduservice.entity.frontvo.CourseWebVO;
import com.cucn.eduservice.service.EduChapterService;
import com.cucn.eduservice.service.EduCourseService;
import com.cucn.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 前台课程列表与详情前端控制器
 *
 * @author KouChaoJie
 * @since: 2021/10/12 10:01
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrdersClient ordersClient;

    /**
     * 条件查询带分页查询课程
     *
     * @param limit         每页记录数
     * @param current       当前页
     * @param courseFrontVO 条件查询封装对象
     */
    @PostMapping("/getFrontCourseList/{current}/{limit}")
    public Result getFrontCourseList(@PathVariable long current, @PathVariable long limit,
                                     @RequestBody(required = false) CourseFrontVO courseFrontVO) {
        Page<EduCourse> pageCourse = new Page<>(current, limit);
        Map<String, Object> map = courseService.getCourseFrontList(pageCourse, courseFrontVO);

        return Result.ok().data(map);
    }

    /**
     * 前台查询课程详情的方法
     *
     * @param courseId 课程id
     */
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public Result getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id,查询出课程基本信息
        CourseWebVO courseWebVO = courseService.getBaseCourseInfo(courseId);

        //根据课程id查询出章节和小节部分
        List<ChapterVO> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        //根据课程id和用户id查询当前课程是否已经支付过了
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberIdByJwtToken)) {
            throw new GuliException(28004, "请先登录");
        }

        boolean buyCourse = ordersClient.isBuyCourse(courseId, memberIdByJwtToken);

        return Result.ok().data("courseWebVO", courseWebVO).data("chapterVideoList", chapterVideoList).data("isBuy", buyCourse);
    }

    /**
     * 根据课程id查询课程信息
     *
     * @param id 课程id
     * @return 获取到的用于远程调用课程信息对象
     */
    @PostMapping("/getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseWebVO courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo, courseWebVoOrder);

        return courseWebVoOrder;
    }
}
